/*
 * Created by JFormDesigner on Sun Nov 03 16:23:40 CST 2024
 */

package com.dhx.view.RuKu;

import com.dhx.model.RuKuMain;
import com.dhx.service.RuKuService;

import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author 25188
 */
public class QueryRuKuFrm extends JFrame {
    private RuKuService ruKuService = new RuKuService();
    public QueryRuKuFrm() {
        initComponents();
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);

        ArrayList<RuKuMain> ruKuList = ruKuService.getRuKuList(new RuKuMain());
        for (RuKuMain ruKu : ruKuList) {
            Object[] rowData = {ruKu.getId(),ruKu.getRukuname(),ruKu.getPrice(),ruKu.getGysName(),ruKu.getRkdate()};
            model.addRow(rowData);
        }
    }

    private void queryRKbtn(ActionEvent e) {
        // TODO add your code here
        String text = textField1.getText();
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        model.setRowCount(0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuname(text);

        ArrayList<RuKuMain> ruKuList = ruKuService.getRuKuList(ruKuMain);
        for (RuKuMain ruKu : ruKuList) {
            Object[] rowData = {ruKu.getId(),ruKu.getRukuname(),ruKu.getPrice(),ruKu.getGysName(),ruKu.getRkdate()};
            model.addRow(rowData);
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here  获取选中行
        int row = table1.getSelectedRow();
        //获取行的数据
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        int chooseId = (int) model.getValueAt(row, 0);
        String name = (String) model.getValueAt(row, 1);
        textField2.setText(name);
    }

    private void updateRKbtn(ActionEvent e) {
        // TODO add your code here  修改事件
        String name = textField2.getText();
        int row = table1.getSelectedRow();
        DefaultTableModel model = (DefaultTableModel) table1.getModel();
        int chooseId = (int) model.getValueAt(row, 0);
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setId(chooseId);
        ruKuMain.setRukuname(name);
        Boolean aBoolean = ruKuService.updateRuKu(ruKuMain);
        if (aBoolean) {
            JOptionPane.showMessageDialog(null,"修改成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        queryRKbtn = new JButton();
        textField1 = new JTextField();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        textField2 = new JTextField();
        updateRKbtn = new JButton();

        //======== this ========
        setTitle("\u67e5\u8be2\u5165\u5e93\u5355");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5165\u5e93\u5355\u540d\u79f0");

        //---- queryRKbtn ----
        queryRKbtn.setText("\u67e5\u8be2");
        queryRKbtn.addActionListener(e -> queryRKbtn(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null, null},
                    {null, null, null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u79f0", "\u603b\u4ef7", "\u4f9b\u5e94\u5546\u540d\u79f0", "\u4e0b\u5355\u65f6\u95f4"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5165\u5e93\u5355\u540d\u79f0");

        //---- updateRKbtn ----
        updateRKbtn.setText("\u4fee\u6539");
        updateRKbtn.addActionListener(e -> updateRKbtn(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(118, 118, 118)
                            .addComponent(label1)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE)
                            .addGap(42, 42, 42)
                            .addComponent(queryRKbtn))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(85, 85, 85)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(117, 117, 117)
                            .addComponent(label2)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
                            .addGap(68, 68, 68)
                            .addComponent(updateRKbtn)))
                    .addContainerGap(160, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(queryRKbtn)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 19, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(updateRKbtn))
                    .addGap(17, 17, 17))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JButton queryRKbtn;
    private JTextField textField1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JButton updateRKbtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
